package com.hxrfid.web.controller.project;

import cn.dev33.satoken.annotation.SaIgnore;
import com.hxrfid.common.annotation.Log;
import com.hxrfid.common.annotation.RepeatSubmit;
import com.hxrfid.common.core.controller.BaseController;
import com.hxrfid.common.core.domain.PageQuery;
import com.hxrfid.common.core.domain.R;
import com.hxrfid.common.core.page.TableDataInfo;
import com.hxrfid.common.core.validate.AddGroup;
import com.hxrfid.common.core.validate.EditGroup;
import com.hxrfid.common.enums.BusinessType;
import com.hxrfid.common.utils.poi.ExcelUtil;
import com.hxrfid.project.domain.bo.BsConfigBo;
import com.hxrfid.project.domain.vo.BsConfigVo;
import com.hxrfid.project.service.IBsConfigService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 配置中心
 *
 * @author hxrfid
 * @date 2025-06-21
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/project/config")
public class BsConfigController extends BaseController {

    private final IBsConfigService iBsConfigService;

    /**
     * 查询配置中心列表
     */
//    @SaCheckPermission("project:config:list")
    @SaIgnore
    @GetMapping("/list")
    public TableDataInfo<BsConfigVo> list(BsConfigBo bo, PageQuery pageQuery) {
        return iBsConfigService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出配置中心列表
     */
//    @SaCheckPermission("project:config:export")
    @Log(title = "配置中心", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(BsConfigBo bo, HttpServletResponse response) {
        List<BsConfigVo> list = iBsConfigService.queryList(bo);
        ExcelUtil.exportExcel(list, "配置中心", BsConfigVo.class, response);
    }

    /**
     * 获取配置中心详细信息
     *
     * @param id 主键
     */
//    @SaCheckPermission("project:config:query")
    @GetMapping("/{id}")
    public R<BsConfigVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iBsConfigService.queryById(id));
    }

    /**
     * 新增配置中心
     */
//    @SaCheckPermission("project:config:add")
    @Log(title = "配置中心", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody BsConfigBo bo) {
        return toAjax(iBsConfigService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改配置中心
     */
//    @SaCheckPermission("project:config:edit")
    @Log(title = "配置中心", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody BsConfigBo bo) {
        return toAjax(iBsConfigService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除配置中心
     *
     * @param ids 主键串
     */
//    @SaCheckPermission("project:config:remove")
    @Log(title = "配置中心", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iBsConfigService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }
}
